import axios from "axios";

const service = axios.create({
  baseURL:'http://192.168.116.86:8080',//代理服务器
  timeout:10000,
  headers:{
    'Content-Type': 'application/json',
  }
})
 // 请求拦截器
service.interceptors.request.use((config) => {
  const token = localStorage.getItem('token')
  if(token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
},(error) => {
  return Promise.reject(error)
})

// 响应拦截器
service.interceptors.response.use(
  response => {
    const res = response.data
    return res
  },
  error => {
    console.error('请求失败:', {
      url: error.config?.url,
      status: error.response?.status,
      data: error.response?.data,
      message: error.message
    })
    
    return Promise.reject(error)
  }
)


export function get(url,params = {},config = {}) {
  return service({
    url,
    method:'GET',
    params,
    ...config
  })
}

export const post = (url,data,config={}) => {
  return service({
    url,
    method:'POST',
    data,
    ...config
  })
}
export default service